﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace YoutubeDownloader
{
    public partial class Settings : Form
    {
        public Setting Setting;
        public Settings(Setting currentSetting)
        {
            Setting = currentSetting;
            InitializeComponent();
        }
        
        private void CancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void addAuthorButton_Click(object sender, EventArgs e)
        {
            authorList.Items.Add(addAuthorText.Text);
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            if( fileDialog.ShowDialog() == DialogResult.OK)
            {
                moviePlayerPath.Text = fileDialog.FileName;
            }
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            autoDownload.Checked = Setting.AutoDownload;
            autoRefresh.Checked = Setting.AutoRefresh;

            moviePlayerPath.Text = Setting.MediaPlayerPath;

            authorList.Items.Clear();
            
            if(Setting.Authors != null && Setting.Authors.Count() != 0)
            {
                foreach (string author in Setting.Authors)
                {
                    authorList.Items.Add(author);
                }
            }

            crawlPeriod.SelectedItem = Setting.CrawlPeriod;
            formatPreferred.SelectedItem = Setting.FormatPreferred;
        }

        private void ApplyButton_Click(object sender, EventArgs e)
        {

            Setting.AutoDownload = autoDownload.Checked;
            Setting.AutoRefresh = autoRefresh.Checked;
            Setting.MediaPlayerPath = moviePlayerPath.Text;

            Setting.Authors = new List<string>();
            foreach (ListViewItem listViewItem in authorList.Items)
            {
                Setting.Authors.Add(listViewItem.Text);
            }

            Setting.CrawlPeriod = crawlPeriod.SelectedItem.ToString();
            Setting.FormatPreferred = formatPreferred.SelectedItem.ToString();

            DialogResult = DialogResult.OK;
        }

        private void removeAuthorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(authorList.SelectedItems.Count > 0)
            {
                authorList.Items.RemoveAt(authorList.SelectedItems[0].Index);
            }
        }
    }

    [Serializable()]
    public class Setting
    {
        public string FormatPreferred;
        public string CrawlPeriod;
        public string CrawlPeriodUrlParameter
        {
            get
            {
                switch(CrawlPeriod)
                {
                    case "Day":
                        return "today";
                    case "Week":
                        return "this_week";
                    case "Month":
                        return "this_month";
                    case "All":
                        return "all_time";
                    default:
                        return "today";
                }
            }
        }
        public bool AutoDownload;
        public bool AutoRefresh;
        public string MediaPlayerPath;
        public List<string> Authors;
        public List<string> DownloadedVideoId;

        public bool Serialize(string settingPath)
        {
            bool rVal = true;

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Setting));
            TextWriter textWriter = new StreamWriter(settingPath);
            xmlSerializer.Serialize(textWriter, this);
            textWriter.Close();
            
            return rVal;
        }

        public static Setting Deserialize(string settingPath)
        {
            Setting setting = new Setting();
            try
            {
                // Create a new file stream for reading the XML file
                FileStream fileStream = new FileStream(settingPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Setting));

                // Load the object saved above by using the Deserialize function
                setting = (Setting)xmlSerializer.Deserialize(fileStream);

                // Cleanup
                fileStream.Close();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to deserialize config: " + ex);

                setting.FormatPreferred = "720p";
                setting.MediaPlayerPath = "";
                setting.AutoRefresh = true;
                setting.CrawlPeriod = "Month";
                setting.Authors = new List<string>();
                setting.DownloadedVideoId = new List<string>();
                setting.Authors.Add("HuskyStarcraft");
                setting.Authors.Add("PsyStarcraft");
                setting.Authors.Add("Day9Tv");
                setting.Authors.Add("PomfEtThud");
                setting.Authors.Add("QCStarcraft");
            }

            return setting;
        }
    }
}
